package com.itestor.iptv.modules.common.service.impl;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import com.itestor.iptv.modules.common.bean.RegionManageQuery;
import com.itestor.iptv.modules.common.dao.TbRegionManageDao;
import com.itestor.iptv.modules.common.domain.RegionManage;
import com.itestor.iptv.modules.common.service.RegionManageService;
import com.itestor.iptv.modules.common.vo.RegionManageBean;
import com.itestor.iptv.modules.common.vo.RegionManageOBJ;
import com.itestor.iptv.utils.BeanConverter;

/**
 * 
 * @author Mr.li
 * 
 */
public class RegionManageServiceImpl implements RegionManageService {

    private static final Logger logger = Logger
            .getLogger(RegionManageServiceImpl.class);

    private TbRegionManageDao tbRegionManageDao;

    public RegionManageOBJ get(String username) {
        RegionManageOBJ regionManageObj = null;
        RegionManage regionManage = tbRegionManageDao.find(username);

        if (regionManage != null) {
            regionManageObj = convertToRegionManageOBJ(regionManage);
        }
        return regionManageObj;
    }

    public RegionManageOBJ get(int id) {
        RegionManageOBJ region = null;
        RegionManage tregion = tbRegionManageDao.find(id);
        if (tregion != null) {
            region = convertToRegionManageOBJ(tregion);
        }
        return region;
    }

    @SuppressWarnings("unchecked")
    public void queryRegionManageList(RegionManageQuery regionManageQuery) {
        Collection<RegionManageOBJ> regionlist = new ArrayList<RegionManageOBJ>();
        StringBuilder hql = new StringBuilder();

        hql.append("from RegionManage t where 1=1 ");        
        if (!StringUtils.isEmpty(regionManageQuery.getRegionName())) {
            hql.append(" and t.regionName like '%"
                    + regionManageQuery.getRegionName() + "%'");
        }
        
        if (null != regionManageQuery.getIdRegion()) {
            hql.append(" and t.idRegion =" + regionManageQuery.getIdRegion());
        }        

        hql.append(" order by t.regionName ");

        Query query = tbRegionManageDao.getHibernateSession().createQuery(
                hql.toString());

        if (regionManageQuery.getPagerCount() == -1) {
            regionManageQuery.setPagerCount(query.list().size());
        }

        query.setFirstResult(regionManageQuery.getPagerOffset());
        query.setMaxResults(regionManageQuery.getPagerPerPage());

        for (Iterator it = query.iterate(); it.hasNext();) {
            regionlist.add(convertToRegionManageOBJ((RegionManage) it.next()));
        }

        regionManageQuery.setQueryResults(regionlist);
    }

    /**
     * 新增区局
     * @param region
     */
    public void add(RegionManageOBJ region) {
        tbRegionManageDao.create(convertToRegionManage(region));
    }

    /**
     * 删除区局
     * @param ids
     */
    public void delete(int[] ids) {
        for (int id : ids) {
            tbRegionManageDao.delete(id);
        }
    }

    /**
     * 修改区局
     * @param region
     */
    public void modify(RegionManageOBJ region) {
        if (tbRegionManageDao.find(region.getIdRegion()) != null) {
            tbRegionManageDao.update(convertToRegionManage(region));
        }
    }

    /**
     * 获取所有区域列表
     * @return List
     */
    public List<RegionManageOBJ> getRegionManageList() {
        List<RegionManageOBJ> regionManageList = new ArrayList<RegionManageOBJ>();
        Collection<RegionManage> regionList = tbRegionManageDao.findAll();
        for (RegionManage tregion : regionList) {
            regionManageList.add(convertToRegionManageOBJ(tregion));
        }
        return regionManageList;
    }

    /**
     * 获取区局Map集合
     * @return
     */
   public  Map<Integer,RegionManage> getBelongToRegionMap(){
        return tbRegionManageDao.getBelongToRegionMap();
    }
    
    /**
     * @return
     */
    public List<RegionManage> getRootRegionList() {
        return tbRegionManageDao.getRootRegionList();
    }

    /**
     * @param parentRegionId
     * @return
     */
    public List<RegionManageBean> getRegionListByParentid(Integer parentRegionId) {
        return tbRegionManageDao.getChildRegionList(parentRegionId);
    }

    public List<RegionManage> getRegionListByParentId(Integer prentRegionId){
    	return tbRegionManageDao.getRegionListByParentId(prentRegionId);
    }
    
    
    /**
     * @param tregion RegionManage(po)
     * @return region RegionManageOBJ(vo)
     */
    public RegionManageOBJ convertToRegionManageOBJ(RegionManage tregion) {
        RegionManageOBJ region = new RegionManageOBJ();
        BeanConverter.copyProperties(region, tregion);
        return region;
    }

    /**
     * @param region
     * @return
     */
    public RegionManage convertToRegionManage(RegionManageOBJ region) {
        RegionManage tregion = new RegionManage();
        BeanConverter.copyProperties(tregion, region);
        return tregion;
    }

    public void setTbRegionManageDao(TbRegionManageDao pTbRegionManageDao) {
        tbRegionManageDao = pTbRegionManageDao;
    }

}
